import lejos.nxt.SensorPort;
import lejos.nxt.ColorLightSensor;
import lejos.nxt.UltrasonicSensor;
import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.comm.RConsole;
import lejos.nxt.LCD;
import lejos.robotics.Colors;

public class Presentation implements ButtonListener, Runnable {
	private UltrasonicSensor uSensor;
	private ColorLightSensor cSensor;	
	private Screen uScreen;
	private Screen cScreen;

	public Presentation() {
		uSensor = new UltrasonicSensor(SensorPort.S2);
		cSensor = new ColorLightSensor(SensorPort.S1, ColorLightSensor.TYPE_COLORFULL);
		Button.ESCAPE.addButtonListener(this);
		System.out.println("Opening Bluetooth");
		RConsole.openBluetooth(40000);
		LCD.clear();
		uScreen = new Screen("Ultra", 0);
		cScreen = new Screen("Color", 1);
		uScreen.run();	
		uScreen.reportState();
		cScreen.run();
		cScreen.reportState();
	}

	// from ButtonListener
	public void buttonPressed(Button b) {
		LCD.clear();
		//Going to check if the button is ESCAPE
		RConsole.close();
		System.exit(0);
	}

	public void buttonReleased(Button b) {
	}
	// end

	public void ReadUltrasonic() {
		int distance = uSensor.getDistance();
		uScreen.drawInt(distance);
		RConsole.println("Ultra : " + distance);
	}

	public void ReadColor() {
		Colors.Color col = cSensor.readColor();
		cScreen.drawString(col.name());
		RConsole.println("Color : " + col.name());
	}

	public void run() {
		for(;;) {
			ReadUltrasonic();
			ReadColor();
			try {
				Thread.sleep(500);
			} catch(Exception e) {};
		}
	}

	static void main(String[] args) {
		Presentation tmp = new Presentation();
		tmp.run();
		
	}
}
